8.8 Smarter computer playing Tic-Tac-Toe

  1. Motivations
    • Try the 'Person vs computer (Not smart)' Tic-Tac-Toe game in TRU Board Games Playground.
    • Does the computer play well? Can you beat the computer?
    • How to make the computer play better?
    • There is one more thing that you need to know how to use TRU Board Games library.

  2. TRU Board Games library
    • In 'lab6_bgp_ttt.js' in the previous section, you saw an TRUGameBoard object is created for the game.
    • Game boards are generated and supported by TRU Board Games library.
    • Let's study how to use the library.
  3. Lab 7.
    • Let's change the menu 'You vs computer (Not smart)' to 'You vs smart computer'. You also need to be careful with the id of the menu item and the src value for 'lab7_bgp_ttt.js'.
  4. Lab 8.
    • Let's change the menu 'You vs computer (Not smart)' to 'You vs smart computer'.
    • The above html program includes 'TRU Board Games library' and 'lab8_bgp_ttt.js'. This 'js' file includes JavaScript code for the board game.
    • The function start_one_person_tic_tac_toe() in 'lab8_bgp_ttt.js' starts with
      • Creation of a game board
      • Registration of event handlers
      • Code:
        var board = new TRUGameBoard(SIZE, SIZE, "borderLine", 400, 400, "LightGrey");
        board.draw('game-board');  // 'game-board' is the id of an <div> in which the game board is displayed.
                
        // Event handler registration for mouse 'click'
        board.cellsEventListener('click', function(e) {
            var pos = board.position(e);  // pos: {row:r, col:c}
                    
            // Person
            // If there is not 'X' or 'O' mark, i.e., not occupied
            if (board.cellContent(pos.row, pos.col) != 'X' && board.cellContent(pos.row, pos.col) != 'O' ) {
                ttt_mark(board, pos.row, pos.col, 'X', X_O_SIZE);  // Mark the cell with 'X'
                        
                // Now computer's turn
                ttt_computer_turn(board, X_O_SIZE);
            }
                    
        });
                
        // For coloring on the mouse movement
        board.cellsEventListener('mouseenter', function(e) {
            var pos = board.position(e);
            var current_color = board.cellBackgroundColor(pos.row, pos.col);
            board.cellMark2(pos.row, pos.col, current_color);
            board.cellBackgroundColor(pos.row, pos.col, 'LightSteelBlue');
        });
        board.cellsEventListener('mouseleave', function(e) {
            var pos = board.position(e);
            var previous_color = board.cellMark2(pos.row, pos.col);
            board.cellBackgroundColor(pos.row, pos.col, previous_color);
        });
        
    • The above function invokes
      • ttt_mark() to mark a clicked cell
      • ttt_computer_turn() to let the program take the trun
    • Let's complete ttt_computer_turn() in 'lab8_bgp_ttt.js' so that the computer can play better. We'd better start with an algorithm how to defend.
      • Algorithm:
        Check if all marked; if so, return;  // It is implemented.
        Defense first:  // You need to complete some parts.
            For each empty cell,
                Check if the user can win with the cell;
                If so, use the cell and return;
        Offense:  // It is implemented.
            Find the best position and use it;
        
      • lab8_bgp_ttt.js

  5. Learning outcomes